package com.example.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.common.HttpStatusConstants;
import com.example.common.R;
import com.example.mapper.ConEmployeeServiceMapper;
import com.example.mapper.UserMapper;
import com.example.model.req.AddEmployeeReq;
import com.example.model.req.AdminUserLoginReq;
import com.example.model.req.UpdatePriceReq;
import com.example.model.resp.ListAdminEmployeeResp;
import com.example.service.UserService;

@ResponseBody
@RestController
@RequestMapping("/admin")
public class AdminUserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ConEmployeeServiceMapper conEmployeeServiceMapper;
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/login")
    public R<String> amdinUserLogin(@RequestBody AdminUserLoginReq adminUserLoginReq) {
        return userService.adminUserLogin(adminUserLoginReq);
    }

    @PostMapping("/addEmployee")
    public R<String> addEmployee(@RequestBody AddEmployeeReq addEmployeeReq) {
        return userService.addEmployee(addEmployeeReq);
    }

    @GetMapping("/listEmployee")
    public R<List<ListAdminEmployeeResp>> listAdminEmployee(@RequestParam("pageSize") Integer pageSize,
            @RequestParam("currentPage") Integer currentPage) {
        return userService.listAdminEmployee(pageSize, currentPage);
    }

    @GetMapping("/totalEmployee")
    public R<Integer> totalEmployee() {
        return R.success(conEmployeeServiceMapper.totalEmployee());
    }

    @PostMapping("/updatePrice")
    public R<String> updatePrice(@RequestBody() UpdatePriceReq updatePriceReq) {
        try {
            userMapper.updatePrice(updatePriceReq.getPrice(), updatePriceReq.getId());
            return R.success();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(HttpStatusConstants.INTERNAL_SERVER_ERROR, e.getMessage());
        }
    }

    @DeleteMapping("/deleteEmployee/{employeeId}")
    public R<String> deleteEmployee(@PathVariable("employeeId") Integer employeeId) {
        try {
            userMapper.deleteById(employeeId);
            conEmployeeServiceMapper.deleteById(employeeId);
            return R.success();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(HttpStatusConstants.INTERNAL_SERVER_ERROR, e.getMessage());
        }
    }
}
